package com.sky.controller.admin;

import com.sky.anno.AutoFill;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@RequestMapping("/admin/common")
@Slf4j
@Api(tags = "公用接口")
public class CommonController {

    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 文件上传
     * @param file
     * @return
     */
    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file){
        log.info("文件上传到oss:{}",file.getOriginalFilename());
        String name = file.getOriginalFilename();
        String fileName = UUID.randomUUID().toString() + name.substring(name.lastIndexOf("."));

        try {
            String upload = aliOssUtil.upload(file.getBytes(), fileName);
            return Result.success(upload);
        }catch (Exception e){
            log.error("文件上传出错了：{}",e.getMessage());
            return Result.error("文件上传错误");
        }



    }


}
